package com.example.archive.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.archive.entity.ArchiveApprovalRequest;
import com.example.archive.entity.User;
import com.example.archive.service.ArchiveApprovalRequestService;
import com.example.archive.service.UserService;
import com.example.archive.util.PageParam;
import com.example.archive.util.Result;
import com.example.archive.util.UserContext; // Assuming UserContext is still used for current user ID
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/archive-approval")
public class ArchiveApprovalRequestController {

    @Autowired
    private ArchiveApprovalRequestService requestService;

    @Autowired
    private UserService userService; // Inject UserService

    @PostMapping("/submit")
    public Result<Boolean> submitRequest(@RequestBody ArchiveApprovalRequest request) {
        // Set requesterId from UserContext
        Integer currentUserId = UserContext.getUserId();
        if (currentUserId == null) {
            return Result.fail(401, "用户未登录");
        }
        request.setRequesterId(currentUserId);
        return requestService.submitRequest(request);
    }

    @GetMapping("/list")
    public Result<IPage<ArchiveApprovalRequest>> listRequests(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer requesterId,
            @RequestParam(required = false) Integer approverId,
            @RequestParam(required = false) String search) {
        
        Integer currentUserId = UserContext.getUserId();
        if (currentUserId == null) {
            return Result.fail(401, "用户未登录");
        }

        Result<User> userResult = userService.getUserById(currentUserId);
        if (userResult.getCode() != 200 || userResult.getData() == null) {
            return Result.fail(401, "无法获取用户信息");
        }
        String userRole = userResult.getData().getRole();

        Integer effectiveRequesterId = null;
        Integer effectiveApproverId = null;

        // Apply role-based filtering
        if ("普通员工".equals(userRole)) {
            effectiveRequesterId = currentUserId; // 普通员工只能看自己提交的
        } else if ("档案管理员".equals(userRole) || "企业领导".equals(userRole)) {
            // 档案管理员和企业领导可以看所有，不限制requesterId或approverId
            // 如果需要企业领导只看待审批的或者自己审批的，可以在这里细化逻辑
        }

        PageParam pageParam = new PageParam(pageNum, pageSize, search);
        return requestService.listRequests(pageParam, status, effectiveRequesterId, effectiveApproverId);
    }

    @PostMapping("/approve/{requestId}")
    public Result<Boolean> approveRequest(@PathVariable Integer requestId, @RequestBody(required = false) String notes) {
        Integer approverId = UserContext.getUserId();
        if (approverId == null) {
            return Result.fail(401, "用户未登录");
        }
        return requestService.approveRequest(requestId, approverId, notes);
    }

    @PostMapping("/reject/{requestId}")
    public Result<Boolean> rejectRequest(@PathVariable Integer requestId, @RequestBody(required = false) String notes) {
        Integer approverId = UserContext.getUserId();
        if (approverId == null) {
            return Result.fail(401, "用户未登录");
        }
        return requestService.rejectRequest(requestId, approverId, notes);
    }

    @GetMapping("/detail/{requestId}")
    public Result<ArchiveApprovalRequest> getRequestDetail(@PathVariable Integer requestId) {
        return requestService.getRequestById(requestId);
    }
} 